﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmCustomer : Form
    {
        DialogResult dlgResult;
        CustomerInfo cusinf;
        public frmCustomer(int _customer_ID)
        {
            InitializeComponent();
            cusinf = new CustomerInfo();
            UISetting.LoadCustomerType(ref cboCustomerType);
            txtCode.Text = UISetting.AutoFillCustomerCode();
            if (_customer_ID > 0)
            {
                cusinf = CustomerDB.GetInfo(_customer_ID);
                
                InfoToForm();
            }
            cusinf.Customer_ID = _customer_ID;
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        private void FormToInfo()
        {
            cusinf.Customer_CustomerTypeID = Convert.ToInt32(cboCustomerType.SelectedValue);
            cusinf.Customer_Code = txtCode.Text;
            cusinf.Customer_Name = txtName.Text;
            cusinf.Customer_Address = txtAddress.Text;
            cusinf.Customer_Phone = txtPhone.Text;
            cusinf.Customer_Fax = txtFax.Text;
            cusinf.Customer_TaxNo = txtTaxNo.Text;
            cusinf.Customer_Contact = txtContact.Text;
            cusinf.Customer_Remaining = Convert.ToDecimal(txtRemaining.Text);

        }
        private void InfoToForm()
        {
            cboCustomerType.SelectedValue = Convert.ToInt32(cusinf.Customer_CustomerTypeID);
            txtCode.Text = cusinf.Customer_Code;
            txtName.Text = cusinf.Customer_Name;
            txtAddress.Text = cusinf.Customer_Address;
            txtPhone.Text = cusinf.Customer_Phone;
            txtFax.Text = cusinf.Customer_Fax;
            txtTaxNo.Text = cusinf.Customer_TaxNo;
            txtContact.Text = cusinf.Customer_Contact;
            txtRemaining.Text = Convert.ToDecimal(cusinf.Customer_Remaining).ToString("N1");
        }
        private bool CheckValid()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Tên khách hàng trống! Hãy nhập lại", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            FormToInfo();
            if (cusinf.Customer_ID > 0)//Update
            {
                if (CustomerDB.Exist(cusinf.Customer_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (CustomerDB.Update(cusinf))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else //Create
            {
                if (CustomerDB.Exist(cusinf.Customer_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                cusinf.Customer_ID = CustomerDB.Insert(cusinf);
                if (cusinf.Customer_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Create_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmCustomer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtCode.Text = UISetting.AutoFillCustomerCode();
            txtName.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtContact.Text = "";
            txtFax.Text = "";
            txtTaxNo.Text = "";
            txtRemaining.Text = "0.0";
            cboCustomerType.SelectedIndex = 0;
            cusinf.Customer_ID = 0;
        }

        private void frmCustomer_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.Escape:
                    this.Dispose();
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }

        private void txtRemaining_Leave(object sender, EventArgs e)
        {
            decimal dec = 0;
            if (!Decimal.TryParse(txtRemaining.Text, out dec) || dec < 0)
                txtRemaining.Undo();
            else
                txtRemaining.Text = dec.ToString("N1");
        }
    }
}